package com.zfjt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.ClientmessageBean;
import com.zfjt.entity.OrderBean;
import com.zfjt.entity.ReturnBean;
import com.zfjt.packnum.PackNum00024;
import com.zfjt.service.ClientmessageBeanService;
import com.zfjt.utils.StringUtil;


/**
 * 展示本账号所有订单
 * @author xk
 * @version 1.0
 * @date 2017年4月07日
 */
@Controller
public class OrderListController{

	private ReturnBean mReturn;
	
	@Autowired
	private ClientmessageBeanService mClientMessage;
	
	private Map<String, String> fhdata; 
	
	private List<ClientmessageBean> mAllData;
	
	private List<OrderBean> mData;
	
	//当前页面
	private int mPage;
		
	//一页的数据个数
	private int mPageSize;
	
	@RequestMapping(value="00024")
	@ResponseBody
	public ReturnBean AddBrabch(HttpSession hseesion){
		mAllData = new ArrayList<>();
		mData = new ArrayList<>();
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		PackNum00024 num00024 = JSON.parseObject(data,PackNum00024.class); 	
		mPage = Integer.parseInt(num00024.getPage());
		mPageSize = Integer.parseInt(num00024.getPagesize());
		List<ClientmessageBean> mList = mClientMessage.selectAll();
		mAllData.clear();
		for (int i = 0; i < mList.size(); i++) {
			ClientmessageBean dg = mList.get(i);
			if (num00024.getType2().equals("0")) {
				if (num00024.getBrabch().equals("2")) {
					if (dg.getAffiliationWangdian().equals("2")) {
						if (dg.getType().equals(num00024.getType())) {//把相应的类型返回回去	
							String [] str = StringUtil.convertStrToArray2(dg.getName());
							dg.setName(str[0]);
							mAllData.add(dg);
						}
					}
				}else {
					if (dg.getType().equals(num00024.getType())) {//把相应的类型返回回去	
						String [] str = StringUtil.convertStrToArray2(dg.getName());
						dg.setName(str[0]);
						mAllData.add(dg);
					}
				}
					
			}else{
				if (dg.getAffiliationYewu().equals(num00024.getAccount())) {//返回本账号订单
					if (dg.getType().equals(num00024.getType())) {//把相应的类型返回回去
						String [] str = StringUtil.convertStrToArray2(dg.getName());
						dg.setName(str[0]);
						mAllData.add(dg);
					}	
				}
			}
		}
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {//校验码错误
			mReturn.setCode("001");
			mReturn.setPortnum("00024");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{	
			int num = mAllData.size();
			if(mPageSize > num){
				for (int i = mAllData.size() - 1; i >= 0 ; i--) {
					ClientmessageBean dg = mAllData.get(i);
					OrderBean fh = new OrderBean(dg.getId(),dg.getName(), dg.getMobile(), dg.getType(),
							dg.getAddtime(),dg.getMoney(),dg.getCharge(),dg.getRejectImg(),dg.getProduct(),dg.getGenre(),dg.getAffiliationWangdian());
					mData.add(fh);
				}
			}else {
				for (int i = 0; i < mPageSize; i++) {
					ClientmessageBean dg = mAllData.get((num -(mPage - 1)*mPageSize) - 1 - i);
					OrderBean fh = new OrderBean(dg.getId(),dg.getName(), dg.getMobile(), dg.getType(),
							dg.getAddtime(),dg.getMoney(),dg.getCharge(),dg.getRejectImg(),dg.getProduct(),dg.getGenre(),dg.getAffiliationWangdian());
					mData.add(fh);
					
				}
			}
			if(mPageSize > num){
				mReturn.setCode("003");
			}else if(mPage < num/mPageSize){
				mReturn.setCode("000");
			}else{
				mReturn.setCode("003");
			}
			mReturn.setPortnum("00024");
			mReturn.setData(JSON.toJSONString(mData));
		}
		return mReturn;
	}
}
